#include <stdio.h>
#include <MyString.h>
#include "1.h"


typedef struct student
{
    MyStr name;
    int id;
    int age;
}Stu;

typedef struct Teacher
{
    MyStr name;
    int age;
    DArray StuArray;
}Teach;

Stu* CreateStu(const char *name, const int id, const int age)
{
    Stu *s =(Stu*)malloc(sizeof(Stu));
    if(s == NULL)
        return NULL;
    s->id = id;
    s->age = age;
    s->name = GetString(name);
    return s;
}

void ClearStu(void *ptr)
{
    Stu *s = (Stu*)ptr;
    ClearString(&s->name);
    free(s);
}

void PrintStu(void *ptr)
{
    Stu *s = (Stu *)ptr;
    printf("学生学号：%d 学生姓名：%s 学生年龄：%d\n",s->id,s->name.string,s->age);
}

bool SortByAge(void *ptr1, void *ptr2)
{
    Stu *s1 = (Stu *)ptr1;
    Stu *s2 = (Stu *)ptr2;
    if(s1->age > s2->age)
        return true;
    return false;
}

bool SortByName(void *ptr1,void *ptr2)
{
    Stu *s1 = (Stu *)ptr1;
    Stu *s2 = (Stu *)ptr2;
    if(strcmp(s1->name.string,s2->name.string) > 0)
        return true;
    return false;
}

bool CheckAge(void *ptr1, void *ptr2)
{
    int age = (long)ptr1;
    Stu *s = (Stu *)ptr2;
    if(s->age >= age)
        return true;
    return false;
}

Teach *CreateTeach(const char *name, int age)
{
    Teach *t = (Teach *)malloc(sizeof(Teach));
    if(t == NULL)
    {
        return NULL;
    }
    t->age = age;
    t->name = GetString(name);
    InitDynamicArray(&t->StuArray);
    return t;
}

void ClearTeach(void *ptr)
{
    Teach *t = (Teach *)ptr;
    ClearString(&t->name);
    //数组清空
    t->StuArray.ClearDArray(&t->StuArray,ClearStu);
    free(t);
}

void AddStudent(Teach *t,Stu *s)
{
    t->StuArray.InsertArrayTail(&t->StuArray,s);
}

void PrintTeach(void *ptr)
{
    Teach *t = (Teach *)ptr;
    printf("老师名字：%s 老师年龄：%d\n",t->name.string,t->age);
    printf("老师的学生列表：\n");
    t->StuArray.DisplayDynamicArray(&t->StuArray,PrintStu);
}

bool CheckNameContain(void *ptr1,void *ptr2)
{
    char *name = (char *)ptr1;
    Teach *t = (Teach *)ptr2;
    if(IsContains(&t->name,name) == true)
        return true;
    return false;
}


